<?php


namespace app\admin\controller;


use app\common\service\ServiceSubplat;
use think\Request;
use think\Db;

class Suborg extends Base
{
    public function __construct()
    {
        parent::__construct();
        $this->isLogin();
    }

    /**
     * @route('suborg/suborglist')
     * 经销商列表
     */
    public function suborglist()
    {
        $suborgModel = new \app\common\model\Shops();


        // 获取搜索条件
        $get['sbg_name'] = input('get.sbg_name');
//        判断用户类型
        $isdisplay = true;
        if(!empty($this->_spfid)){$get['spf_id'] = $this->_spfid;$isdisplay = false;}
        // 后台账号数据
        $rs_data = $suborgModel->get_list_page($get,'a.state,a.id desc','a.*,b.spf_name', $this->limit);
        if(!empty($this->_spfid)){unset($get['spf_id']);}
        $tvar = array(
            "title" => "商铺列表",
            "time"  => time(),
            "data"  => $rs_data,
            "get"   => $get,
            "isdisplay" => $isdisplay,
        );
        $page = $rs_data->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('suborg/suborglist');
    }


    /**
     * @route('suborg/boxesuborg')
     */
    public function BoxeSuborg(Request $request)
    {
        $suborgModel = new \app\common\model\Shops();
        $subplatModel = new \app\common\model\Subplatform();

        $subplatServ = new ServiceSubplat();

        if(\request()->isAjax()){
            $param = $request->param();

            // 处理
            $rs = $subplatServ->DealSuborg($param);
            if($rs === false){
                $this->error($subplatServ->getError());
            }
            $this->success('操作成功','/suborg/suborglist');
        }

        $Id = $_GET['sbg_id'];
        $admin = null;
        if($Id != 0){
            $admin = $suborgModel->getOne(['sbg_id' => $Id],'*');
        }

        // 判断当前账号是否总平台账号
        if($this->isHqAdmin()){
            $subplat_data = $subplatModel->get_list('spf_id,spf_name');
        }else{
            $subplat_data[] = $subplatModel->getOne(['spf_id' => $this->_spfid],'spf_id,spf_name');
        }

        $tvar = array(
            "title" => "添加账号",
            "time"  => time(),
            "sbg_id"   => $Id,
            "subplat"   => $subplat_data,
        );
        $this->assign("data", $tvar);
        $this->assign("suborg", $admin);
        return $this->fetch('suborg/boxesuborg');
    }


    /**
     * @route('suborg/delsuborg2')
     * 商铺状态修改
     */
    public function DelAdmin()
    {
        $Id = $_GET['id'];

        $rs = Db::table('ddt_shops')
            ->where('id',$Id)
            ->update(['state'=>'2']);
        if($rs === false){
            $this->error("操作失败");
        }
        $this->success('操作成功 ');
    }

}